﻿
using Dapper;
using HBase;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public abstract class InComponent<CS, CSD> :GkTransaction where CS : AbsCoreStock where CSD : AbsCoreStockDetail, new() {
        public String boxCode;
        public int stn;
        void continueTask(IDbConnection conn ,IDbTransaction trans ,CoreTask ct) {
            String sql = $"select * from Core_task_param where task_id=@taskId  and type={( int ) TaskParamType.托盘搬运} and status <= {( int ) CoreTaskParamStatus.下发完成}";
            List<CoreTaskParam> list = conn.Query<CoreTaskParam>(sql ,new { taskId = ct.id } ,trans).ToList();
            if( list.Count > 0 ) {
                return;
            }
            CS cs = conn.GetById<CS>(ct.stockId ,trans);
            if( cs.status == ( int ) StockStatus.盘库中 ) {
                ParamContent pc = new ParamContent { };
                TaskPersistenceUtil.moveCoreTaskParam(conn ,trans ,pc ,ct.id ,stn ,ct.src ,0);
            }
        }
        protected override TransRes Execute(IDbConnection conn ,IDbTransaction tran) {
            List<CoreTask> coreTaskList = checkCodeInWork(conn ,tran);
            if( coreTaskList.Count == 1 ) {
                continueTask(conn ,tran ,coreTaskList[0]);
                return new TransRes();
            } else if( coreTaskList.Count > 1 ) {

                return new TransRes(true ,boxCode + "at" + stn + "working 不能入库!");
            }
            before(conn ,tran);
            CS cs = getStock(conn ,tran);

            if( cs.status != 0 ) {
                return new TransRes(boxCode + "at" + stn + "状态不能入库!" + cs.status);
            }
            // 可能原来入库站台，与正式入库口不一直。
            cs.locId = stn;
            long stockId = cs.id;
            List<CSD> stockDetailsList = CsdUtil.GetStockDetailList<CSD>(conn ,tran ,stockId);
            foreach( CSD csd in stockDetailsList ) {
                if( csd.storeStatus == ( int ) StockDetailStatus.待盘库 ) {

                    return new TransRes("盘库中不能入库");
                }
            }
            CoreWhLoc loc = SelectLocUtil.selectLoc(conn ,tran ,stockDetailsList);
            if( loc == null ) {

                return new TransRes("货位获取异常");
            }

            long goodDiff = LocConfig.getGoodDiff(conn ,tran ,stockDetailsList[0] ,cs);
            TaskPersistenceUtil.createInTask(goodDiff ,cs ,( int ) loc.id ,conn ,tran);

            return afterIntask(conn ,tran ,cs ,stockDetailsList ,loc);
        }
        protected virtual TransRes afterIntask(IDbConnection conn ,IDbTransaction trans ,CS cs ,List<CSD> stockDetailsList ,CoreWhLoc loc) {
            return new TransRes();
        }
        protected virtual void before(IDbConnection connection ,IDbTransaction transaction) {
        }

        protected virtual List<CoreTask> checkCodeInWork(IDbConnection connection ,IDbTransaction transaction) {
            string sql = "select * from Core_Task where  status <9 and  status>0 ";

            if( !String.IsNullOrWhiteSpace(boxCode) ) {
                sql += "  and box_Code=@boxCode  ";
            } else {
                sql += "  and src =  " + stn;
            }
            List<CoreTask> coreTaskList = connection.Query<CoreTask>(sql ,new {
                boxCode
            } ,transaction).ToList();
            return coreTaskList;
        }

        public virtual CS getStock(IDbConnection Connection ,IDbTransaction transaction) {
            string sql = @"select * from Core_stock where  status in (0,1,2,3,4,5,6,7)   and box_Code=@boxCode  ";
            List<CS> stockList = Connection.Query<CS>(sql ,new {
                boxCode
            } ,transaction).ToList();
            if( stockList.Count == 0 ) {
                String info = boxCode + "没有库位异常stock!";
                throw new Exception(info);
            }
            if( stockList.Count > 1 ) {
                String info = boxCode + "重复的库位异常stock!";
                throw new Exception(info);
            }
            CS cs = stockList[0];
            return cs;
        }
    }
}
